package ocumed.teams.teamd.application;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import ocumed.teams.IMediver;
import ocumed.teams.IPatient;
import ocumed.teams.OcumedFactory;
import ocumed.teams.PersistenceFactory;

/**
 *
 * @author Willi
 */
public class AppVerordnung {

    IPatient m_patient;
    private boolean m_isDirty;

    public AppVerordnung() {
    }

    public void addMedikament(int id) {
        IMediver ver = m_patient.createiVerordnung();

        ver.setiMedikament(OcumedFactory.getInstance().getiMedikament(id));


        ver.setMediVerVon(zeroDate(new Date(), 0));
        ver.setMediVerBis(zeroDate(new Date(), 23));
        ver.setMediVerMenge(1);

        m_patient.addiVerordnung(ver);

        m_isDirty = true;
    }

    private Date zeroDate(Date date, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public void deleteVerordnung(int id) {
        // könnte man auch in der domänenschicht
        m_patient.deleteiVerordnung(id);

        m_isDirty = true;
    }

    public VerordnungTableModel getTableModel() {
        VerordnungTableModel v;
        if (m_patient != null) {
            v = new VerordnungTableModel(m_patient.getiMedivers());
        } else {
            v = new VerordnungTableModel(new HashSet<IMediver>());
        }

        return v;
    }

    public void load(int patient) {
        m_patient = OcumedFactory.getInstance().getiPatient(patient);

        m_isDirty = false;
    }

    public void save() {

        PersistenceFactory.getInstance().put(m_patient);

        m_isDirty = false;
    }

    public boolean isDirty() {
        return m_isDirty;
    }
}
